package com.casic.ops.entity.dto.pipeline.datastructure.Logger;


import com.casic.ops.consts.user.UserHolder;
import com.casic.ops.entity.dto.UserDTO;
import com.casic.ops.utils.OpsSnowFlake;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * 节点执行记录
 * @author cww
 * @date 2022/6/17 11:24:39
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class NodeLogger implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private long uuid;
    private Date gmtCreate;
    private Date gmtModified;
    private String creator;
    private String modifier;
    private String deleted;
    /**
     * 流水线执行记录uuid
     */
    private long loggerUuid;
    /**
     * 节点uuid
     */
    private String nodeUuid;
    /**
     * 节点中文名称
     */
    private String nodeName;
    /**
     * 存储数据正文
     */
    private String logger;
    /**
     * 执行时间
     */
    private Date executeStartTime;
    /**
     * 结束时间
     */
    private Date executeEndTime;

    /**
     * 最终状态
     */
    private String finalStatus;

    /**
     * 存储日志
     */
    private StringBuilder sb = new StringBuilder();

    /**
     * 初始化
     */
    public void init(){
        UserDTO user=UserHolder.get();
        if(user!=null){
            this.setCreator(user.getUsername());
            this.setModifier(user.getUsername());
        }
        Date date = new Date();
        this.setGmtCreate(date);
        this.setGmtModified(date);
        this.setUuid(OpsSnowFlake.getInstance().nextId());
    }
    /**
     * 序列化
     */
    public void serializable(){
        logger = sb.toString();
    }

    public void append(String logger){
        sb.append(logger);
    }
}
